home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillips / arotate.c < prev    next >
Encoding:
Text File  |  1994-02-26  |  2.8 KB  |  103 lines

  1.  
  2.      /*******************************************
  3.      *
  4.      *   arotate(..
  5.      *
  6.      *   This routine performs rotation about
  7.      *   any point m,n.
  8.      *
  9.      *   The basic equations are:
  10.      *
  11.      *   new x = x.cos(a) - y.sin(a)
  12.      *           -m.cos(a) + m + n.sin(a)
  13.      *
  14.      *   new y = y.cos(a) + x.sin(a)
  15.      *           -m.sin(a) - n.cos(a) + n
  16.      *
  17.      *******************************************/
  18.  
  19. arotate(in_name, out_name, the_image, out_image,
  20.         il, ie, ll, le, angle,
  21.         m, n, bilinear)
  22.    char   in_name[], out_name[];
  23.    float  angle;
  24.    int    bilinear, il, ie, ll, le;
  25.    short  the_image[ROWS][COLS],
  26.           out_image[ROWS][COLS],
  27.           m, n;
  28. {
  29.    double cosa, sina, radian_angle, tmpx, tmpy;
  30.    int    i, j, new_i, new_j;
  31.    struct tiff_header_struct image_header;
  32.  
  33.    create_file_if_needed(in_name, out_name, out_image);
  34.  
  35.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  36.  
  37.  
  38.       /* the following magic number is from
  39.          180 degrees divided by pi */
  40.    radian_angle = angle/57.29577951;
  41.    cosa  = cos(radian_angle);
  42.    sina  = sin(radian_angle);
  43.  
  44.       /**************************
  45.       *
  46.       *   Loop over image array
  47.       *
  48.       **************************/
  49.  
  50.    printf("\n");
  51.    for(i=0; i<ROWS; i++){
  52.       if( (i%10) == 0) printf("%d ", i);
  53.       for(j=0; j<COLS; j++){
  54.  
  55.      /******************************************
  56.      *
  57.      *   new x = x.cos(a) - y.sin(a)
  58.      *           -m.cos(a) + m + n.sin(a)
  59.      *
  60.      *   new y = y.cos(a) + x.sin(a)
  61.      *           -m.sin(a) - n.cos(a) + n
  62.      *
  63.      *******************************************/
  64.  
  65.          tmpx = (double)(j)*cosa    -
  66.                 (double)(i)*sina    -
  67.                 (double)(m)*cosa    +
  68.                 (double)(m)         +
  69.                 (double)(n)*sina;
  70.  
  71.          tmpy = (double)(i)*cosa    +
  72.                 (double)(j)*sina    -
  73.                 (double)(m)*sina    -
  74.                 (double)(n)*cosa    +
  75.                 (double)(n);
  76.  
  77.          new_j = tmpx;
  78.          new_i = tmpy;
  79.  
  80.          if(bilinear == 0){
  81.             if(new_j < 0       ||
  82.                new_j >= COLS   ||
  83.                new_i < 0       ||
  84.                new_i >= ROWS)
  85.                out_image[i][j] = FILL;
  86.             else
  87.                out_image[i][j] =
  88.                 the_image[new_i][new_j];
  89.          }  /* ends if bilinear */
  90.          else{
  91.             out_image[i][j] = 
  92.                bilinear_interpolate(the_image,
  93.                                     tmpx, tmpy);
  94.          }  /* ends bilinear if */
  95.  
  96.       }  /* ends loop over j */
  97.    }  /* ends loop over i */
  98.  
  99.    write_array_into_tiff_image(out_name, out_image,
  100.                                il, ie, ll, le);
  101.  
  102. }  /* ends arotate */
  103.